package com.gujia.controller;

import com.gujia.model.Message;
import com.gujia.model.User;
import com.gujia.model.ViewObject;
import com.gujia.service.MessageService;
import com.gujia.service.UserService;
import com.gujia.util.AnswerUtil;
import com.gujia.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by GuJia on 2017/6/30.
 */
@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;


    @RequestMapping(path = {"/msg/addMessage"}, method = {RequestMethod.POST})
    @ResponseBody
    public String addMessage(Model model, @RequestParam("toName") String toName,
                             @RequestParam("content") String content){


        try{
            if(LoginUtil.getUser()==null){
                return AnswerUtil.getJSONString(999,"un login !");
            }

        User user = userService.getUserByName(toName);

        if (user== null){
            return AnswerUtil.getJSONString(1,"Send User not exiest !");
        }
        Message message = new Message();
        message.setContent(content);
        message.setCreatedDate(new Date());
        message.setToId(user.getId());
        message.setFromId(LoginUtil.getUserId());
        messageService.addMessage(message);
        }catch (Exception e ){
            e.printStackTrace();
            return AnswerUtil.getJSONString(1,"Message send failed !");
        }

        return AnswerUtil.getJSONString(0);

    }


    @RequestMapping(path = {"/msg/list"}, method = {RequestMethod.GET})
    public String conversationDetail(Model model) {
        try {
            int localUserId = LoginUtil.getUserId();
            List<ViewObject> conversations = new ArrayList<ViewObject>();
            List<Message> conversationList = messageService.getConversationList(localUserId, 0, 10);
            for (Message msg : conversationList) {
                ViewObject vo = new ViewObject();
                vo.set("conversation", msg);
                int targetId = msg.getFromId() == localUserId ? msg.getToId() : msg.getFromId();
                User user = userService.getUser(targetId);
                vo.set("user", user);
                vo.set("unread", messageService.getConvesationUnreadCount(localUserId, msg.getConversationId()));
                conversations.add(vo);
            }
            model.addAttribute("conversations", conversations);
        } catch (Exception e) {
            //logger.error("获取站内信列表失败" + e.getMessage());
            e.printStackTrace();
        }
        return "letter";
    }

    @RequestMapping(path = {"/msg/detail"}, method = {RequestMethod.GET})
    public String conversationDetail(Model model, @RequestParam("conversationId") String conversationId) {
        try {
            List<Message> conversationList = messageService.getConversationDetail(conversationId, 0, 10);
            List<ViewObject> messages = new ArrayList<>();
            for (Message msg : conversationList) {
                ViewObject vo = new ViewObject();
                vo.set("message", msg);
                User user = userService.getUser(msg.getFromId());
                if (user == null) {
                    continue;
                }
                vo.set("headUrl", user.getHeadUrl());
                vo.set("userId", user.getId());
                messages.add(vo);
            }
            model.addAttribute("messages", messages);
        } catch (Exception e) {
            //logger.error("获取详情消息失败" + e.getMessage());
            e.printStackTrace();
        }
        return "letterDetail";
    }
}
